import axios from 'axios';
export const MethodType = {
    GET: 'GET',
    POST: 'POST',
    PUT: 'PUT',
    DELETE: 'DELETE',
    PATCH:'PATCH'
};
    
export function sendAll (arr) { // 顺序和请求发送的顺序相同，使用 axios.spread 分割成多个单独的响应对象
    if (Object.prototype.toString.call(arr) === '[object Array]') {
      return axios.all(arr).then(axios.spread(function (...res) { // axios.all 是axios的静态方法，不是实例上的方法
        // 请求全部都执行完成
        return Promise.resolve(res)
      }))
    } else {
      const error = new Error('参数错误！')
      try {
        throw error
      } catch (e) {
        // console.log(e)
      }
    }
  }
  /**
   * 模块说明:有api_token的请求
   */
export const request = async(url,method,params,config) => {
  switch(method){
    case "GET":
      // try {
      //   const res = await axios.get(url, params);
      //   console.log('resresresres',res)
      //   return await Promise.resolve(res);
      // }
      // catch (err) {

      //   return  await Promise.reject(err ||'接口请求失败-------------');
      // }

      return await axios.get(url, { params: params }).then(res => Promise.resolve(res)).catch(err => Promise.reject(err));
    case "POST":
      return await axios.post(url, params).then(res => Promise.resolve(res)).catch(err =>Promise.reject(err));
    case "PUT":
      return await axios.put(url, params).then(res => Promise.resolve(res)).catch(err =>Promise.reject(err));
    case "DELETE":
      return await axios.delete(url, { params: params }).then(res => Promise.resolve(res)).catch(err =>Promise.reject(err));
    case "PATCH":
      return await axios.patch(url, params).then(res => Promise.resolve(res)).catch(err =>Promise.reject(err));
  }
};



let base = '';

//测试使用

export const POST = async (url, params) => {
  console.log('params----',params)
  const getTimestamp = new Date().getTime();
  try {
    const res = await axios.post(`${base}${url}?timer=${getTimestamp}`, params);
    return await Promise.resolve(res);
  }
  catch (err) {
    return await Promise.reject(err);
  }
}
export const GET = async (url, params) => {
  const getTimestamp = new Date().getTime();
  try {
    const res = await axios.get(`${base}${url}?timer=${getTimestamp}`, { params: params });
    return await Promise.resolve(res);
  }
  catch (err) {
    return await Promise.reject(err);
  }
}

export const PUT = async (url, params) => {
  try {
    const res = await axios.put(`${base}${url}`, params);
    return await Promise.resolve(res);
  }
  catch (err) {
    return await Promise.reject(err);
  }
}

export const DELETE = async (url, params) => {
  try {
    const res = await axios.delete(`${base}${url}`, { params: params });
    return await Promise.resolve(res);
  }
  catch (err) {
    return await Promise.reject(err);
  }
}

export const PATCH = async (url, params) => {
  try {
    const res = await axios.patch(`${base}${url}`, params);
    return await Promise.resolve(res);
  }
  catch (err) {
    return await Promise.reject(err);
  }
}